package com.example.shiwu.adapter;

import static androidx.core.app.ActivityCompat.startActivityForResult;
import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.shiwu.ItemInfoDetailsActivity;
import com.example.shiwu.ItemInfoEditActivity;
import com.example.shiwu.ItemInfoListActivity;
import com.example.shiwu.R;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.service.ItemsService;

import java.util.ArrayList;
import java.util.List;

public class MySimpleChangeAdapter extends BaseAdapter {

    private List<Items> listItems = new ArrayList<Items>();
    private int resourceId;

    private Context mContext;
    private Integer itemId;
    private ItemsService itemsService;
    private ViewGroup myparent;
    private int position_old = 0;
    private View old_view;

    public MySimpleChangeAdapter(Context mContext, List<Items> listItems, int resource) {
        this.resourceId = resource;
        this.mContext = mContext;
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view;
        final ViewHolder viewHolder;
        myparent = parent;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder(view, position);
            view.setTag(viewHolder);
            view.findViewById(R.id.item_change_list).setTag(position);
            viewHolder.itemsName.setText(listItems.get(position).getName());
            viewHolder.itemsLocaltion.setText(listItems.get(position).getLocation());
            viewHolder.itemsNum.setText(listItems.get(position).getNum());
            viewHolder.itemsDeviceType.setText(listItems.get(position).getDeviceType());

            viewHolder.changeItemsStatus.setVisibility(View.INVISIBLE);

            if(listItems.get(position).getStatus().equals(1)){
                position_old = position;
                old_view = view;
                view.findViewById(R.id.item_change_list).setBackgroundResource(R.drawable.bg_itemlist_change);
                view.findViewById(R.id.change_items_status).setVisibility(View.VISIBLE);
                view.findViewById(R.id.item_now).setBackgroundResource((R.drawable.bg_blue_white_left));
            }

        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.itemsName.setText(listItems.get(position).getName());
        viewHolder.itemsLocaltion.setText(listItems.get(position).getLocation());
        viewHolder.itemsNum.setText(listItems.get(position).getNum());
        viewHolder.itemsDeviceType.setText(listItems.get(position).getDeviceType());

        viewHolder.changeItemsStatus.setVisibility(View.INVISIBLE);

        if(listItems.get(position).getStatus().equals(1)){
            position_old = position;
            old_view = view;
            view.findViewById(R.id.item_change_list).setBackgroundResource(R.drawable.bg_itemlist_change);
            view.findViewById(R.id.change_items_status).setVisibility(View.VISIBLE);
            view.findViewById(R.id.item_now).setBackgroundResource((R.drawable.bg_blue_white_left));
        }

        viewHolder.item_change_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                ViewParent vv = v.getParent();
                v.setEnabled(true);
                System.out.println("id ================= " + v.getId());
                int position = (Integer) v.getTag();
                itemId = listItems.get(position).getId();
                changeItem(itemId,view);
                //changeAfterBackground(myparent);
                v.setBackgroundResource(R.drawable.bg_itemlist_change);
                v.findViewById(R.id.change_items_status).setVisibility(View.VISIBLE);
                v.findViewById(R.id.item_now).setBackgroundResource((R.drawable.bg_blue_white_left));
                //getAllChildren(view);
            }
        });

        return view;
    }


//    private ArrayList<View> getAllChildren(View v) {
//
//        if (!(v instanceof ViewGroup)) {
//            ArrayList<View> viewArrayList = new ArrayList<View>();
//            viewArrayList.add(v);
//            return viewArrayList;
//        }
//
//        ArrayList<View> result = new ArrayList<View>();
//
//        ViewGroup viewGroup = (ViewGroup) v;
//        for (int i = 0; i < viewGroup.getChildCount(); i++) {
//
//            View child = viewGroup.getChildAt(i);
//
//            ArrayList<View> viewArrayList = new ArrayList<View>();
//            viewArrayList.add(v);
//            viewArrayList.addAll(getAllChildren(child));
//
//            result.addAll(viewArrayList);
//        }
//        return result;
//    }

    private void changeAfterBackground(ViewGroup parent){
        View view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
//        LinearLayout layout = (LinearLayout) v.getParent();
//        RelativeLayout item_change_list = (RelativeLayout) view.getTag(position);
//        Integer count = item_change_list.getChildCount();
        RelativeLayout item_change_list = view.findViewById(R.id.item_change_list);
        item_change_list.setBackgroundResource(R.drawable.bg_itemlist_change);
//        int count = item_change_list.size();
//        System.out.println("count ================ "+count);
    }
    private void changeItem(Integer id,View view) {
        itemsService = new ItemsService();
        Items items = new Items();
        items.setStatus(0);
        itemsService.updade(items, new Items());

        items.setStatus(1);
        items.setId(id);
        itemsService.updade(items, new Items(id));
        if(listItems.get(position_old).getStatus().equals(1)){
            view.findViewById(R.id.item_change_list).setBackgroundResource(R.drawable.bg_itemlist);
            view.findViewById(R.id.change_items_status).setVisibility(View.VISIBLE);
            view.findViewById(R.id.item_now).setBackgroundResource((R.drawable.bg_white));

            old_view.findViewById(R.id.item_change_list).setBackgroundResource(R.drawable.bg_itemlist);
            old_view.findViewById(R.id.change_items_status).setVisibility(View.INVISIBLE);
            old_view.findViewById(R.id.item_now).setBackgroundResource((R.drawable.bg_white));
            old_view=view;
        }
    }

    class ViewHolder {

        TextView detailsSub, itemsName, itemsLocaltion, itemsNum, itemsDeviceType,changeItemsStatus;
        RelativeLayout item_change_list;

        public ViewHolder(View view, int position) {

            itemsName = view.findViewById(R.id.change_items_name);
            itemsLocaltion = view.findViewById(R.id.change_items_localtion_val);
            itemsNum = view.findViewById(R.id.change_items_num_val);
            itemsDeviceType = view.findViewById(R.id.change_items_device_type_val);
            changeItemsStatus = view.findViewById(R.id.change_items_status);
            item_change_list = view.findViewById(R.id.item_change_list);
        }

    }

    //    完成按钮
    public interface OnItemCompletedListener {
        void onCompletedClick(int position);
    }

    private OnItemCompletedListener onItemCompletedListener;

    public void setOnItemCompletedListener(OnItemCompletedListener onItemCompletedListener) {
        this.onItemCompletedListener = onItemCompletedListener;
    }

    //    取消完成
    public interface OnItemCancelCompletedListener {
        void onCancelCompletedClick(int position);
    }

    private OnItemCancelCompletedListener onItemCancelCompletedListener;

    public void setOnItemCancelCompletedListener(OnItemCancelCompletedListener onItemCancelCompletedListener) {
        this.onItemCancelCompletedListener = onItemCancelCompletedListener;
    }

}
